package com.swift.widget;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.swift.R;

public class TranslucentImageView extends android.support.v7.widget.AppCompatImageView {
	private String textString="dsfdsdf";
	private float fontSize;
	private int fontColor;

	public TranslucentImageView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public TranslucentImageView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

	public TranslucentImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.TranslucentImageView);
		textString = a.getString(R.styleable.TranslucentImageView_status);
		fontSize = a.getDimension(R.styleable.TranslucentImageView_transTextSize, 20);
		fontColor = a.getColor(R.styleable.TranslucentImageView_transTextColor,
				Color.WHITE);
		a.recycle();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		float radius = 10;
	    Path path = new Path();
	    RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
	    path.addRoundRect(rect, radius, radius, Path.Direction.CW);
	    canvas.clipPath(path);
		super.onDraw(canvas);
		textString = getTextString();
		if (TextUtils.isEmpty(textString)) {
			return;
		}
		
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		Rect targetRect = new Rect(0, getHeight()/4 * 3, getWidth(),getHeight()); 
		paint.setColor(new Color().parseColor("#16b0f4"));
		paint.setStyle(Style.FILL);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
		canvas.drawRect(targetRect, paint);

		paint.setTextSize(fontSize);
		FontMetricsInt fontMetrics = paint.getFontMetricsInt();
		int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
		paint.setTextAlign(Paint.Align.CENTER);
		paint.setColor(fontColor);
		
		canvas.drawText(textString, targetRect.centerX(), baseline, paint);
	}

	public String getTextString() {
		return textString;
	}

	public void setTextString(String textString) {
		this.textString = textString;
	}

	public float getFontSize() {
		return fontSize;
	}

	public void setFontSize(float fontSize) {
		this.fontSize = fontSize;
	}

	public int getFontColor() {
		return fontColor;
	}

	public void setFontColor(int fontColor) {
		this.fontColor = fontColor;
	}
	
	

}
